/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.statement;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionNode;

public class WhileStatementNode extends StatementNode {
	private ExpressionNode expression;
	private StatementListNode statements;
	
	public WhileStatementNode(ExpressionNode expr,
								StatementListNode statements) {
		this.expression=expr;
		this.statements=statements;
	}

	public ExpressionNode getExpression() {
		return expression;
	}

	public StatementListNode getStatements() {
		return statements;
	}

	@Override
	public void generateTAC() throws IOException {
		int labIndex=tacCounter.getNextLabelIndex();
		tacCounter.getWhileLabelStack().push(labIndex);
		writeTAC("label", "L"+labIndex+"_start", "", "");		// L1_start:
		expression.generateTAC();
		int tIndex=tacCounter.getTermIndex()-1;
		writeTAC("if", "_t"+tIndex, "", "L"+labIndex+"_do");	// if expression!=0 then goto L1_do
		writeTAC("goto", "", "", "L"+labIndex+"_end");			// goto L1_end
		writeTAC("label", "L"+labIndex+"true", "", "");			// L1_do:
		statements.generateTAC();
		writeTAC("goto", "", "", "L"+labIndex+"_start");		// goto L1_start
		writeTAC("label", "L"+labIndex+"_end", "", "");			// L1_end:
		tacCounter.getWhileLabelStack().pop();
	}
}
